﻿using System.Text.Json;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using TianlalaHSB.Models;
using TianlalaHSB.Utils;

namespace TianlalaHSB.Views;

/// <summary>
///     MerchantAccountView.xaml 的交互逻辑
/// </summary>
public partial class MerchantAccountView : Page
{
    public MerchantAccountView()
    {
        InitializeComponent();

        DataContext = new AccountQuery();
    }

    private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        e.Handled = !Number.IsInteger(e.Text);
    }

    private async void SubmitButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            var subAcctNoVal = subAcctNo.Text.Trim();
            var subAcctNameVal = subAcctName.Text.Trim();
            if (Validation.GetHasError(subAcctNo)) throw new Exception("资金账号格式不正确");

            // 准备POST请求的数据 
            var postData = new MerchantAccountBalanceQuery
            {
                headReqSerialNo = HttpUtil.GetHeadReqSerialNo(),
                fileFlag = 0,
                start = 1,
                size = 10
            };

            if (!string.IsNullOrEmpty(subAcctNoVal)) postData.subAcctNo = subAcctNoVal;

            if (!string.IsNullOrEmpty(subAcctNameVal)) postData.subAcctName = subAcctNameVal;

            var responseBody = await HttpUtil.Request("03050103A1001", postData);
            var list = JsonSerializer.Deserialize<ResponseList<MerchantAccount>>(responseBody);
            if (list == null) throw new Exception("无数据");

            // 在这里处理获取到的数据，例如更新UI
            RecordListView.ItemsSource = list.dtos;
        }
        catch (Exception ex)
        {
            // 处理异常，例如网络问题或无效的请求等
            MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}